package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealConterllor {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    CategoryService categoryService;

    /*
    * 套餐分页+模糊查询*/
    @GetMapping("/page")
    public R<Page> selectPage(Integer page,Integer pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //创建条件构造器进行查询
        LambdaQueryWrapper<Setmeal> slqy = new LambdaQueryWrapper<>();
        slqy.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //对时间进行排序
        slqy.orderByDesc(Setmeal::getUpdateTime);
        //Setmeal分页数据
        setmealService.page(setmealPage, slqy);
        //将setmealPage的总条数数据copy到setmealDtoPage
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> list = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象copy
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            //获取到分类对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //获取分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }

  /*
  * 新增套餐*/
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    /*
    * 删除套餐*/
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");

    }

    /*
    * 起售和停售*/
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> statusSetMeal(@PathVariable Integer status,@RequestParam List<Long> ids){
        for (Long id : ids) {
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setStatus(status);
            setmealDto.setId(id);
            setmealService.updateById(setmealDto);
        }
        return R.success("操作成功");
    }
    /*
    * 根据套餐id查询套餐数据*/
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto byIdWithDish = setmealService.getByIdWithDish(id);
        return R.success(byIdWithDish);
    }
    /*
    * 修改套餐*/
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }
    /*
    * 根据套餐分类id查询分类下的套餐*/
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.status+'_'+#setmeal.categoryId_")
    public R<List<Setmeal>> getSetmealById(Setmeal setmeal){

        //创建条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(Setmeal::getStatus, 1);
        //对添加时间进行排序
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        //封装数据到集合

        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);

    }

}
